#include "StdAfx.h"
#include ".\ktvbyteorder.h"



int ChangeByteOrder( BYTE* lpSrc, BYTE* lpDst, int nLength )
{
	ASSERT( lpSrc != NULL && lpDst != NULL && nLength > 0 && nLength % 4 == 0 );

	BYTE* tmp = new BYTE[nLength+10];
	memset( tmp , 0, nLength+10);

	int i = 0;
	int j = 0;
	for( i = 0; i < 4; i++ )
	{
		int nSize = nLength / 4;
		for(j = 0; j < nSize; j++ )
			tmp[ i*nSize + j ] = lpSrc[ j*4 + i ];
	}

	memcpy( lpDst, tmp, nLength );

	delete[] tmp;

	return TRUE;
}

int BackByteOrder( BYTE* lpSrc, BYTE* lpDst, int nLength )
{
	ASSERT( lpSrc != NULL && lpDst != NULL && nLength > 0 && nLength % 4 == 0 );

	BYTE* tmp = new BYTE[nLength+10];
	memset( tmp , 0, nLength+10);


	int i = 0;
	int j = 0;
	for( i = 0; i < 4; i++ )
	{
		int nSize = nLength / 4;
		for(j = 0; j < nSize; j++ )
			tmp[ j*4 + i ] = lpSrc[ i*nSize + j ];
	}

	memcpy( lpDst, tmp, nLength );


	delete[] tmp;

	return TRUE;
}